package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.controls.ToolTip;
	import mx.controls.Tree;
	import mx.events.ListEvent;
	import mx.events.ResizeEvent;
	import mx.managers.ToolTipManager;
	import mx.core.Application;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class Topic{
		private var panel:SuperPanel = new SuperPanel();
		private var tekst:TextArea = new TextArea();
		private var tool:Tool = new Tool();
		private var label1:Label = new Label();
		private var label2:Label = new Label();
		private var c:Tree = new Tree();
		private var service:Mysql = new Mysql();
		private var xmlPom:String = "";
		private var xmlList:XMLList = new XMLList();
		private var noReply:Boolean = false;
		private var newRepyBtn:Button = new Button();
		private var tema:String;		
		private var createToolTip:Boolean = true;
		private var tip:ToolTip;
		
		public function Topic(p:SuperPanel){
			panel = p;
			label1.x = 10;
			label1.y = 27;
			label1.width = panel.width-30;
			label1.height = 25;
			label2.x = 10;
			label2.y = 245;
			label2.width = panel.width-30;
			label2.height = 25;
			tekst.x = 10;
			tekst.y = 45;
			tekst.height=200;
	 		tekst.width=panel.width-30;
	 		tekst.editable = false;
			newRepyBtn.label = "ODGOVORI";
			newRepyBtn.x = panel.width-113;
			newRepyBtn.y = panel.height-45;
			newRepyBtn.addEventListener(MouseEvent.CLICK, onNewTopicBtnClick);
			c.x = 10;
			c.y = 260;
	 		c.height=panel.height-315;
	 		c.width=panel.width-30;
			panel.addChild(c);
			panel.addChild(tekst);
			panel.addChild(label1);
			panel.addChild(label2);
			panel.addChild(newRepyBtn);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
		}
		
		public function overList(event:ListEvent):void{
			if(createToolTip){
	 			c.selectedIndex = event.rowIndex; 
	            if(panel.x+panel.width+280<panel.parentApplication.screen.width){
	            	tip = ToolTipManager.createToolTip(tool.PlainTekst(c.selectedItem.@tekst).substring(0, 255)+"...", panel.x+panel.width-20, panel.y+295+(event.rowIndex*22)) as ToolTip;
             	} else tip = ToolTipManager.createToolTip(tool.PlainTekst(c.selectedItem.@tekst).substring(0, 255)+"...", panel.x, Application.application.parent.mouseY) as ToolTip;
             	createToolTip = false;
        	    tip.setStyle("backgroundColor",0xFFCC00);
        	    tip.width = 300;
   			}
		}

		public function outList(event:ListEvent):void{
			if(!createToolTip) ToolTipManager.destroyToolTip(tip);
			createToolTip = true;
		}
	
		public function onResize(e:ResizeEvent):void{
	 		tekst.width=panel.width-30;
			label1.width = panel.width-30;
	 		c.height=panel.height-315;
	 		c.width=panel.width-30;
			label2.width = panel.width-30;
			if(noReply) tekst.height = panel.height-100;
			newRepyBtn.x = panel.width-113;
			newRepyBtn.y = panel.height-45;
		}
		
		public function show(s:String, n:String, u:String, d:String, v:String, i:Number):void{
			tema = i.toString();
			label1.htmlText = "Temu otvorio: <b>"+u+"</b> dana: <b>"+d+"</b> u <b>"+v+"</b>";
			label2.htmlText = "Odgovori na temu: ";
			tekst.htmlText = tool.DecodeTekst(s);
			service.sql = {type:"SELECT", a1:"k.id_komentar", a2:"k.datum", a3:"k.vrijeme", a4:"u.username", a5:"k.tema", a6:"k.tekst", from:"korisnik u, komentar k", where:"k.username=u.id_username AND k.tema="+i+" ORDER BY k.datum DESC, k.vrijeme DESC"};
	 		service.addEventListener(service.MYSQL_RESULT, onReplyResult);
	 		service.startService();	
		}
		
		public function onReplyResult(e:Event):void{
			service.removeEventListener(service.MYSQL_RESULT, onReplyResult);
			xmlList = service.xml.children();
			
			if(xmlList.length()==0) {
			 	panel.removeChild(c);
			 	panel.removeChild(label2);
			 	tekst.height = panel.height-100;
			 	noReply = true;
			} else {
					xmlPom = "<root naziv='Odgovori' tip='-1'>";
					for (var i:int=0; i<xmlList.length(); i++) {
						xmlPom+="<odgovor tekst='"+xmlList[i].tekst+"'naziv='"+(i+1)+". "+xmlList[i].username+" -> ["+xmlList[i].datum+" / "+xmlList[i].vrijeme+"]' tema='"+xmlList[i].tema+"' id_komentar='"+xmlList[i].id_komentar+"' datum='"+xmlList[i].datum+"' vrijeme='"+xmlList[i].vrijeme+"' username='"+xmlList[i].username+"'></odgovor>";
					}		
					xmlPom+="</root>";
					var xml:XML=new XML(xmlPom);
					var xmlList1:XMLList = xml.children();
					c.dataProvider = xmlList1;
					c.showRoot = false;
					c.showDataTips = true;
					c.labelField = "@naziv";
					c.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onClick);
	 				c.addEventListener(ListEvent.ITEM_ROLL_OVER, overList);
	 				c.addEventListener(ListEvent.ITEM_ROLL_OUT, outList);
			}
		}
		
		public function onClick(e:ListEvent):void{			
			if(!panel.parentApplication.checkExist("ODGOVOR ("+c.selectedItem.@id_komentar+")")&&String(c.selectedItem.@naziv).length>0){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("ODGOVOR ("+c.selectedItem.@id_komentar+")", 0, 400, 500, 300, 0, true, true, "Odgovor"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var reply:Reply = new Reply(s);
	 			service.addEventListener(service.MYSQL_RESULT, onReplyResult);
	 			service.startService();
				s.height = 500;
				reply.show(c.selectedItem.@tekst, c.selectedItem.@username, c.selectedItem.@datum, c.selectedItem.@vrijeme);
			}
		}
		
		public function onNewTopicBtnClick(e:MouseEvent):void{
			if(!panel.parentApplication.checkExist("NOVI ODGOVOR")){
				var s:SuperPanel;
				panel.parentApplication.panels.push(s = tool.createPanel("NOVI ODGOVOR", 0, 400, 400, 400, 100, true, true, "Novi odgovor"));
				panel.parentApplication.addChild(s);
				s.backButton.addEventListener(MouseEvent.CLICK, panel.parentApplication.taskbarButtonClicked);
				s.backButton.addEventListener(MouseEvent.RIGHT_CLICK, panel.parentApplication.taskbarButtonRightClicked);
				var newTopic:NewReply = new NewReply(s);
				newTopic.setParameters(panel.parentApplication.username, tema, panel.title);
			}
		}
	}
}